package com.zhuozhou.jdyc.api;

import android.app.Activity;
import android.app.Application;
import android.content.SharedPreferences;

import com.baidu.location.BDLocation;
import com.zhuozhou.jdyc.bean.Code.ResponseCode;
import com.zhuozhou.jdyc.main.user.bean.UserInfo;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by 方舟 on 2017/5/23.
 * 用户登录等相关信息
 */

public class UserAccountHelper {
    public static UserAccountHelper userAccountHelper;
    public static Application application;
    private static SharedPreferences sp;

    public static String APPNAME = ApplicationHelper.getInstance().getPackageName();

    /**
     * 用户登录是否成功
     */
    private static final String USER_STATE = APPNAME + "_USER_STATE";
    /**
     * 用户信息
     */
    private static final String USER_INFO = APPNAME + "_USER_INFO";

    /**
     * 登录账号
     */
    private static final String USER_ACCOUNT = APPNAME + "USER_ACCOUNT";
    /**
     * 登录密码
     */
    private static final String USER_PASSWORD = APPNAME + "USER_PASSWORD";
    /**
     * 上次登录账号
     */
    private static final String LAST_USER_ACCOUNT = APPNAME + "LAST_USER_ACCOUNT";
    /**
     * api基本参数
     */
    private static final String API_PARAMS = APPNAME + "_api_params";
    /**
     * baseUrl地址
     */
    private static final String BASE_URL = APPNAME + "_base_url";
    /**
     * 搜索记录
     */
    private static final String SEARCH_RECORDS = APPNAME + "_search_records";
    /**
     * 当前城市
     */
    private static final String NEW_CITY = APPNAME + "_new_city";
    /**
     * 当前城市
     */
    private static final String LOCATION = APPNAME + "_location";

    public UserAccountHelper(Application application) {
        UserAccountHelper.application = application;
    }

    public static void init(Application application) {
        if (userAccountHelper == null) {
            userAccountHelper = new UserAccountHelper(application);
        }
        sp = application.getSharedPreferences(APPNAME, Activity.MODE_PRIVATE);
    }

    /**
     * 判断是否登录
     *
     * @return 登录状态
     */
    public static boolean isLogin() {
        return getToken() != null && getUser() != null
                && ((boolean) new SharedPreferencesHelper(application, USER_INFO).getSharedPreference(USER_STATE, false));
    }

    /**
     * 登录保存用户信息
     *
     * @param object    用户相关信息
     * @param isSuccess 是否登录成功
     */
    public static void saveLoginState(Object object, boolean isSuccess) {
        SharedPreferencesHelper sharedPreferencesHelper = new SharedPreferencesHelper(application, USER_INFO);
        sharedPreferencesHelper.putObject(application, object);
        sharedPreferencesHelper.put(USER_STATE, isSuccess);
    }

    /**
     * 获取登录状态
     *
     * @return 是否登录成功
     */
    private static boolean getLoginState() {
        SharedPreferencesHelper sharedPreferencesHelper = new SharedPreferencesHelper(application, USER_INFO);
        return (boolean) sharedPreferencesHelper.getSharedPreference(USER_STATE, false);
    }

    /**
     * 设置用户登录信息为失败，用来下次打开app的时候状态为登录失败
     *
     * @param isSuccess 设置用户登录过期，本地版判断
     */
    public static void saveLoginPast(boolean isSuccess) {
        SharedPreferencesHelper sharedPreferencesHelper = new SharedPreferencesHelper(application, USER_INFO);
        sharedPreferencesHelper.put(USER_STATE, isSuccess);
    }

    /**
     * 获取用户信息
     *
     * @return
     */
    public static UserInfo getUser() {
        return new SharedPreferencesHelper(application, USER_INFO).getObject(application, UserInfo.class);
    }

    /**
     * 退出登录
     */
    public static void exit() {
        SharedPreferencesHelper sharedPreferencesHelper = new SharedPreferencesHelper(application, USER_INFO);
        sharedPreferencesHelper.removeObject(application, UserInfo.class);
        sharedPreferencesHelper.put(USER_STATE, false);
        UserAccountHelper.clearToken();
    }

    /**
     * 保存用户账号和密码
     *
     * @param account  账号
     * @param password 密码
     */
    public static void saveUserMessage(String account, String password) {
        SharedPreferences.Editor editor = sp.edit();
        editor.putString(USER_ACCOUNT, account);
        editor.putString(USER_PASSWORD, password);
        editor.apply();
    }

    public static String getAccount() {
        return sp.getString(USER_ACCOUNT, null);
    }

    public static int getUserID() {
//        if (getUser() == null) return 0;
//        if (getUser().getNav() == null) return 0;
//        return StringUtil.isNotEmpty(getUser().getNav().get(0).getUser_id()) ? getUser().getNav().get(0).getUser_id() : 0;
        return 72;
    }


    public static String getPassword() {
        return sp.getString(USER_PASSWORD, null);
    }

    /**
     * 存储搜索记录
     *
     * @param account
     */
    public static void saveSearchRecords(String account) {
        List<String> list = new ArrayList<>(Arrays.asList(account.split(",")));
        List<String> newList = new ArrayList<>();
        String str = null;
        boolean flag = false;
        for (int i = 0; i < list.size(); i++) {
            for (String s : newList) {
                if (s.equals(list.get(i))) {
                    flag = true;
                    break;
                }
                flag = false;
            }
            if (flag) {
                continue;
            }
            newList.add(list.get(i));
        }
        if (newList.size() == 1) {
            str = newList.get(0);
        } else
            for (int i = 0; i < newList.size(); i++) {
                if (i == 0)
                    str = newList.get(i) + ",";
                else if (i == newList.size() - 1)
                    str += newList.get(i);
                else
                    str += newList.get(i) + ",";
            }
        //str.charAt(i)返回的是char型  所以先加一个""空格 , 转换成String型
        //TreeSet泛型能保证重复的不加入 , 而且有序
        SharedPreferencesHelper sharedPreferencesHelper = new SharedPreferencesHelper(application, USER_INFO);
        sharedPreferencesHelper.put(SEARCH_RECORDS, str);
    }

    /**
     * 获取搜索记录
     *
     * @return
     */
    public static String getSearchRecords() {
        SharedPreferencesHelper sharedPreferencesHelper = new SharedPreferencesHelper(application, USER_INFO);
        return sharedPreferencesHelper.getString(application, SEARCH_RECORDS, null);
    }

    /**
     * 存储当前城市
     *
     * @param city
     */
    public static void saveNewCity(String city) {
        SharedPreferencesHelper sharedPreferencesHelper = new SharedPreferencesHelper(application, USER_INFO);
        sharedPreferencesHelper.put(NEW_CITY, city);
    }

    /**
     * 获取当前城市
     *
     * @return city
     */
    public static String getNewCity() {
        SharedPreferencesHelper sharedPreferencesHelper = new SharedPreferencesHelper(application, USER_INFO);
        return sharedPreferencesHelper.getString(application, NEW_CITY, "德州市");
    }

    /**
     * 存储当前定位坐标
     *
     * @param location
     */
    public static void saveCoordinates(BDLocation location) {
        SharedPreferencesHelper sharedPreferencesHelper = new SharedPreferencesHelper(application, USER_INFO);
        sharedPreferencesHelper.putObject(application, location);
    }

    /**
     * 获取当前定位坐标
     *
     * @return location
     */
    public static BDLocation getCoordinates() {
        SharedPreferencesHelper sharedPreferencesHelper = new SharedPreferencesHelper(application, USER_INFO);
        return sharedPreferencesHelper.getObject(application, BDLocation.class);
    }

    /**
     * 存储上次登录账号
     *
     * @param account 手机号码
     */
    public static void saveLastAccount(String account) {
        SharedPreferencesHelper sharedPreferencesHelper = new SharedPreferencesHelper(application, USER_INFO);
        sharedPreferencesHelper.put(LAST_USER_ACCOUNT, account);
    }

    /**
     * 获取上次登录的账号
     *
     * @return 手机号码或者null
     */
    public static String getLastAccount() {
        SharedPreferencesHelper sharedPreferencesHelper = new SharedPreferencesHelper(application, USER_INFO);
        return sharedPreferencesHelper.getString(application, LAST_USER_ACCOUNT, null);
    }

    /**
     * 本地保存token值
     *
     * @param token token值
     */
    public static void setToken(String token) {
        SharedPreferencesHelper sharedPreferencesHelper = new SharedPreferencesHelper(application, ApiAccountHelper.TOKEN_DATA);
        sharedPreferencesHelper.put(ApiAccountHelper.TOKEN_DATA_STRING, token);
    }

    /**
     * 清理token值
     */
    static void clearToken() {
        SharedPreferencesHelper sharedPreferencesHelper = new SharedPreferencesHelper(application, ApiAccountHelper.TOKEN_DATA);
        sharedPreferencesHelper.clear();
    }

    /**
     * 获取token值
     */
    public static String getToken() {
        SharedPreferencesHelper sharedPreferencesHelper = new SharedPreferencesHelper(application, ApiAccountHelper.TOKEN_DATA);
        return sharedPreferencesHelper.getSharedPreference(ApiAccountHelper.TOKEN_DATA_STRING, null) == null ?
                "e85b47b7-356e-4d16-9c6f-40ae027cb79f" : sharedPreferencesHelper.getSharedPreference(ApiAccountHelper.TOKEN_DATA_STRING, null).toString();
    }
//    /**
//     * 获取token值
//     */
//    public static String getToken() {
//       return "e85b47b7-356e-4d16-9c6f-40ae027cb79f";
//    }

    /**
     * 本地保存token值
     *
     * @param token token值
     */
    public static void setRefreshToken(String token) {
        SharedPreferencesHelper sharedPreferencesHelper = new SharedPreferencesHelper(application, ApiAccountHelper.TOKEN_DATA);
        sharedPreferencesHelper.put(ApiAccountHelper.TOKEN_DATA_STRING_REFRESH, token);
    }

    /**
     * 获取token值
     */
    public static String getRefreshToken() {
        SharedPreferencesHelper sharedPreferencesHelper = new SharedPreferencesHelper(application, ApiAccountHelper.TOKEN_DATA);
        return sharedPreferencesHelper.getSharedPreference(ApiAccountHelper.TOKEN_DATA_STRING_REFRESH, null) == null ?
                null : sharedPreferencesHelper.getSharedPreference(ApiAccountHelper.TOKEN_DATA_STRING_REFRESH, null).toString();
    }

    public static boolean isLoginPast(String code) {
        return ResponseCode.LOGIN_PAST.equals(code)
                || ResponseCode.TOKEN_ERROR.equals(code)
                || ResponseCode.FAILURE.equals(code);
    }

    public static boolean isNoPermission(String code) {
        return ResponseCode.NOPERMISSION.equals(code);
    }


    /**
     * 保存baseUrl
     *
     * @param baseUrl
     */
    public static void saveBaseUrl(String baseUrl) {
        SharedPreferencesHelper sharedPreferencesHelper = new SharedPreferencesHelper(application, API_PARAMS);
        sharedPreferencesHelper.put(BASE_URL, baseUrl);
    }

    /**
     * 获取baseUrl请求地址
     */
    public static String getBaseUrl() {
        SharedPreferencesHelper sharedPreferencesHelper = new SharedPreferencesHelper(application, API_PARAMS);
        return sharedPreferencesHelper.getString(application, BASE_URL, null);
    }

}
